import axios from 'axios'
// import { store } from '../store'
import { Message } from 'element-ui'
// import { getToken } from '../utils'
// import getEnv from "@/utils/env";
// var isVerify = false;
const service = axios.create({
  // baseURL: getEnv(process.env.VUE_APP_SERVER_ENV).apiHost,
  timeout: 5000
})

service.interceptors.request.use(
  (config) => {
    // do something before request is sent
    // config.headers.token = getToken() || ''
    // config.headers.language = getCookie('LANGUAGE') || 'en-us'
    config.headers['wizard-lang'] = window._language || 'en-us'

    // if (config.url.includes('?')) {
    //   config.url = config.url + '&date=' + ((new Date()).getTime()) // 兼容IE11兼容模式请求相同的 URL ，浏览器默认会使用之前的缓存数据，而不是重新请求接口
    // } else {
    //   config.url = config.url + '?date=' + ((new Date()).getTime()) // 兼容IE11兼容模式请求相同的 URL ，浏览器默认会使用之前的缓存数据，而不是重新请求接口
    // }

    return config
  },
  (error) => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)
service.interceptors.response.use(
  (response) => {
    const res = response.data
    // return res;
    if (res.code !== 0) {
      // Message.error(res.msg)
      Message({message:res.msg, type: 'error', customClass: 'z-index-9999'})

    }
    return res
  },
  (error) => {
    if (error && error.response && error.response.status) {
      // console.log(error.response.status)
      switch (error.response.status) {
        case 500:
          // do something...
          break
        case 401:
          // if (!isVerify) {
          //   isVerify = true;
          //   service({
          //     url: '/api/verify-token',
          //     method: 'get',
          //     params: {
          //       token: localStorage.getItem('token')
          //     },
          //   }).then((res) => {
          //     if (res.code == 0) {
          //       localStorage.setItem('account', res.data.account)
          //       localStorage.setItem('token', res.data.user_token)
          //       window.location.reload()
          //       isVerify = false;
          //     } else {
          //         localStorage.setItem('account', '')
          //         localStorage.setItem('token', '')
          //         window.location.reload()
          //         isVerify = false;
          //     }
          //   }).catch( () => {
          //       isVerify = false;
          //   })
          // }
          localStorage.clear()
          window.location.reload()
          break
        default:
          // do something...
          // Message.error(error.message)
          Message({message:error.message, type: 'error', customClass: 'z-index-9999'})
          break
      }
    }

    //
    // debugger
    // console.log(error) //
    // console.log(error) //
    // Message.error(error.message)
    // return
  }
)

export default service
